package com.ggh.jinjilive.ui.home.video;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.ActivityVideoGuanliBinding;
import com.ggh.jinjilive.listener.AddItemOnClickListener;
import com.ggh.jinjilive.listener.AddOnClickListener1;
import com.ggh.jinjilive.mobile.mine.video.PersonalVideoList;
import com.ggh.jinjilive.pagestatus.EmptyCallback;
import com.ggh.jinjilive.pagestatus.LoadingCallback;
import com.ggh.jinjilive.ui.BaseActivity;
import com.ggh.jinjilive.ui.home.video.adapter.VideoGuanLiAdapter;
import com.ggh.jinjilive.utils.ARouterUtils;
import com.ggh.jinjilive.utils.GeneralUtils;
import com.ggh.jinjilive.views.GridItemDecoration;
import com.google.gson.Gson;
import com.gyf.immersionbar.ImmersionBar;
import com.kingja.loadsir.callback.Callback;
import com.kingja.loadsir.core.LoadSir;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Route(path = ARouterUtils.VideoGuanLiActivity)
public class VideoGuanLiActivity extends BaseActivity<ActivityVideoGuanliBinding> implements OnRefreshListener, OnLoadMoreListener,View.OnClickListener, AddItemOnClickListener {
    //适配器
    private VideoGuanLiAdapter videoGuanLiAdapter;
    //选中的数据
    private List<Integer> integerList = new ArrayList<>();

    private int nowPage = 1;
    @Override
    public void initCreate() {
        super.initCreate();
        ImmersionBar.with(this).keyboardEnable(true).init();
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_video_guanli;

    }


    @Override
    public void initView() {
        super.initView();
        loadService = LoadSir.getDefault().register(binding.refreshLayout, new Callback.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loadService.showCallback(LoadingCallback.class);
                //获取订单信息
                onRefresh(binding.refreshLayout);
            }
        });
        loadService.showCallback(LoadingCallback.class);

        setWhiteActionBarStyle("视频管理",true,true);
        //获取状态栏高度
        binding.llSpacing.setPadding(0, BarUtils.getStatusBarHeight(),0,0);
        //刷新
        binding.refreshLayout.setRefreshHeader(new ClassicsHeader(mContext));
        binding.refreshLayout.setRefreshFooter(new ClassicsFooter(mContext));
    }

    @Override
    public void initData() {
        super.initData();
        //清理集合
        integerList.clear();
        //适配器
        videoGuanLiAdapter = new VideoGuanLiAdapter();
        videoGuanLiAdapter.management("管理");
        videoGuanLiAdapter.setAddItemOnClickListener(this);
        binding.recyclerView.setLayoutManager(new GridLayoutManager(mContext,2));
        binding.recyclerView.addItemDecoration(new GridItemDecoration(2, ConvertUtils.dp2px(10),true));
        binding.recyclerView.setAdapter(videoGuanLiAdapter);
        //获取数据
        onRefresh(binding.refreshLayout);
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        //刷新
        binding.refreshLayout.setOnRefreshListener(this);
        //加载
        binding.refreshLayout.setOnLoadMoreListener(this);
        //管理
        binding.managementButton.setOnClickListener(this);
    }



    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.management_Button://管理
                if(binding.managementButton.getText().toString().equals("管理")){
                    binding.managementButton.setText("完成");
                    videoGuanLiAdapter.management("完成");
                }else if(binding.managementButton.getText().toString().equals("完成")){
                    binding.managementButton.setText("管理");
                    videoGuanLiAdapter.management("管理");
                }else if(binding.managementButton.getText().toString().equals("删除")){
                    String video_id = "";
                    for (Integer integer : integerList){
                        if(TextUtils.isEmpty(video_id)){
                            video_id = integer +"";
                        }else {
                            video_id = video_id +"," + integer;
                        }
                    }
                    if(TextUtils.isEmpty(video_id)){
                        ToastUtils.make().show("请选择视频");
                        return;
                    }
                    Map<String, Object> params = new HashMap<>();
                    params.put("video_id", video_id);
                    GeneralUtils.infoDelVideo(params, new AddOnClickListener1() {
                        @Override
                        public void onSkipClicked(Object mObject) {
                            //获取数据
                            onRefresh(binding.refreshLayout);
                        }
                    });
                }
                break;
        }
    }




    @Override
    public void onRefresh(@NonNull @NotNull RefreshLayout refreshLayout) {
        Map<String, Object> params = new HashMap<>();
        nowPage = 1;
        params.put("page", nowPage);
        GeneralUtils.infoVideoList(params, new AddOnClickListener1() {
            @Override
            public void onSkipClicked(Object mObject) {
                binding.refreshLayout.finishRefresh();
                List<PersonalVideoList> personalVideoListList = (List<PersonalVideoList>) mObject;
                if(null != personalVideoListList && personalVideoListList.size() > 0){
                    binding.managementButton.setVisibility(View.VISIBLE);
                    loadService.showSuccess();
                    videoGuanLiAdapter.setAdd(personalVideoListList);
                }else {
                    binding.managementButton.setVisibility(View.GONE);
                    loadService.showCallback(EmptyCallback.class);
                }
            }
        });
    }

    @Override
    public void onLoadMore(@NonNull @NotNull RefreshLayout refreshLayout) {
        Map<String, Object> params = new HashMap<>();
        nowPage = nowPage + 1;
        params.put("page", nowPage);
        GeneralUtils.infoVideoList(params, new AddOnClickListener1() {
            @Override
            public void onSkipClicked(Object mObject) {
                List<PersonalVideoList> personalVideoListList = (List<PersonalVideoList>) mObject;
                if(personalVideoListList.size() > 0){
                    videoGuanLiAdapter.Add(personalVideoListList);
                    binding.refreshLayout.finishLoadMore();
                }else {
                    binding.refreshLayout.finishLoadMoreWithNoMoreData();
                }
            }
        });
    }

    @Override
    public void onSkipClicked(int position, Object mObject) {
        PersonalVideoList personalVideoList = (PersonalVideoList) mObject;
        if(null != personalVideoList){
            if(position == 9999999){//私密
                Map<String, Object> params = new HashMap<>();
                params.put("video_id", personalVideoList.getVideo_id());
                params.put("is_private", personalVideoList.getIs_private());
                GeneralUtils.videoSetPrivate(params, new AddOnClickListener1() {
                    @Override
                    public void onSkipClicked(Object mObject) {
                        //获取数据
                        onRefresh(binding.refreshLayout);
                    }
                });
            }else if(position == 9999998){
                Map<String, Object> params = new HashMap<>();
                params.put("video_id", personalVideoList.getVideo_id());
                params.put("is_comment", personalVideoList.getIs_comment());
                GeneralUtils.videoSetcomment(params, new AddOnClickListener1() {
                    @Override
                    public void onSkipClicked(Object mObject) {
                        //获取数据
                        onRefresh(binding.refreshLayout);
                    }
                });
            }else if(position == 9999997){
                integerList.add(personalVideoList.getVideo_id());
                if(integerList.size() > 0){
                    binding.managementButton.setText("删除");
                }else {
                    binding.managementButton.setText("完成");
                }
                LogUtils.d(new Gson().toJson(integerList));
            }else if(position == 9999996){

                for (int i = 0;i < integerList.size();i++){
                    if(integerList.get(i) == personalVideoList.getVideo_id()){
                        integerList.remove(i);
                        i--;
                    }
                }
                if(integerList.size() > 0){
                    binding.managementButton.setText("删除");
                }else {
                    binding.managementButton.setText("完成");
                }
                LogUtils.d(new Gson().toJson(integerList));
            }
        }
    }
}
